//采购发票
// 打开新增采购发票小窗口
function showStockInvoice() {
	window.location.href("stockInvoice!addInput.action");
} 
		//StockInvoice
function updateStockInvoiceList() {

	//namecheckbox
	//var table=document.getElementById("info_table");
	var checkboxNo = document.getElementsByName("rowlist");
	//
	var delrow = document.getElementById("delrow");
	for (var i = checkboxNo.length - 1; i >= 0; i = i - 1) {
		if (checkboxNo[i].checked) {
			alert("\u786e\u5b9a\u8981\u4fee\u6539\u4f60\u9009\u4e2d\u7684\u8bb0\u5f55\u5417\uff1f");
			delrow.action = "stockInvoiceList!updateInput.action";
			//action	
			delrow.submit();
		    //j=checkboxNo[m].parentElement.parentElement.rowIndex ;
			//table.deleteRow(j);		
		}
	}
}

//
function deleteStockInvoiceList() {

	//namecheckbox
	var table = document.getElementById("info_table");
	var checkboxNo = document.getElementsByName("rowlist");
	var delrow = document.getElementById("delrow");
	var list = new Array();
	var j = 0;
	for (var i = checkboxNo.length - 1; i >= 0; i = i - 1) {
		if (checkboxNo[i].checked) {
			list[j] = checkboxNo[i].parentElement.parentElement.rowIndex;
		}
	}
	if (list.length < 1) {
		//
		alert("\u8bf7\u9009\u62e9\u4f60\u60f3\u8981\u5220\u9664\u7684\u8bb0\u5f55");
	} else {
		if (list.length > 0) {
			if (confirm("\u786e\u5b9a\u8981\u5220\u9664\u5417\uff1f")) {
				delrow.action = "stockInvoiceList!del.action";
				delrow.submit();
		    //j=checkboxNo[m].parentElement.parentElement.rowIndex ;
			//table.deleteRow(j);					
			}//endif
		}
	}//end else
}//end delrow


//StockInvoice
function auditingStockInvoiceList() {

	//namecheckbox
	var table = document.getElementById("info_table");
	var checkboxNo = document.getElementsByName("rowlist");
	//
	var delrow = document.getElementById("delrow");
	for (var i = checkboxNo.length - 1; i >= 0; i = i - 1) {
		if (checkboxNo[i].checked && confirm("\u786e\u5b9a\u8981\u5ba1\u6838\u5417\uff1f")) {
			//alert("u786eu5b9au8981u4feeu6539u4f60u9009u4e2du7684u8bb0u5f55u5417uff1f");
			delrow.action = "stockInvoiceList!auditing.action";
			//action	
			delrow.submit();
		    //j=checkboxNo[m].parentElement.parentElement.rowIndex ;
			//table.deleteRow(j);		
		}
	}
}

function reAuditing(){

	//namecheckbox
	var table = document.getElementById("info_table");
	var checkboxNo = document.getElementsByName("rowlist");
	//
	var delrow = document.getElementById("delrow");
	for (var i = checkboxNo.length - 1; i >= 0; i = i - 1) {
		if (checkboxNo[i].checked && confirm("\u786e\u5b9a\u8981\u5ba1\u6838\u5417\uff1f")) {
			//alert("u786eu5b9au8981u4feeu6539u4f60u9009u4e2du7684u8bb0u5f55u5417uff1f");
			delrow.action = "stockInvoiceList!reAuditing.action";
			//action	
			delrow.submit();
		    //j=checkboxNo[m].parentElement.parentElement.rowIndex ;
			//table.deleteRow(j);		
		}
	}
}

function searchStockInvoiceList() {

	//namecheckbox
	var table = document.getElementById("info_table");
	var checkboxNo = document.getElementsByName("rowlist");
	var delrow = document.getElementById("delrow");
	var list = new Array();
	var j = 0;
	for (var i = checkboxNo.length - 1; i >= 0; i = i - 1) {
		if (checkboxNo[i].checked) {
			list[j] = checkboxNo[i].parentElement.parentElement.rowIndex;
		}
	}
	if (list.length < 1) {
		//
		alert("");
	} else {
		if (list.length > 0) {
			delrow.action = "stockInvoice!searchList.action";
			delrow.submit();
		    //j=checkboxNo[m].parentElement.parentElement.rowIndex ;
			//table.deleteRow(j);					
		}//endif
	}
}//end delrow
function lookStockInvoiceList() {

	//namecheckbox
	var table = document.getElementById("info_table");
	var checkboxNo = document.getElementsByName("rowlist");
	var delrow = document.getElementById("delrow");
	var list = new Array();
	var j = 0;
	for (var i = checkboxNo.length - 1; i >= 0; i = i - 1) {
		if (checkboxNo[i].checked) {
			list[j] = checkboxNo[i].parentElement.parentElement.rowIndex;
		}
	}
	if (list.length < 1) {
		//
		alert("");
	} else {
		if (list.length > 0) {
			delrow.action = "stockInvoice!lookList.action";
			delrow.submit();
		    //j=checkboxNo[m].parentElement.parentElement.rowIndex ;
			//table.deleteRow(j);					
		}//endif
	}
}//end delrow
function delrowInvoice() {
	//namecheckbox
	var table = document.getElementById("info_table");
	var checkboxNo = document.getElementsByName("rowlist");
	var j = 0;
	var k = 0;
	for (var i = checkboxNo.length - 1; i >= 0; i--) {
		if (checkboxNo[i].checked) {
			k++;
			j = checkboxNo[i].parentElement.parentElement.rowIndex;
			//yesno
			//if(confirm(""))
			table.deleteRow(j);
		}
	}
	if (k < 1) {
		//
		alert("");
	}
}
function returnList() {
	var delrow = document.getElementById("delrow");
	delrow.action = "stockInvoiceList!search.action";
	delrow.submit();
}
function addStockInvoiceRow() {
	var t = document.getElementById("mbody");
	var row = t.insertRow(t.rows.length);
	var rowIndex = t.rows.length - 1;
	var col = row.insertCell(0);
	
	col.innerHTML = "<td><input type='hidden' name='stockinvoicedetail' id='stockinvoicedetail"+rowIndex+"'><input type='checkbox' name='checkbox'></td>" ;
	col = row.insertCell(1);
	col.innerHTML = "<td><input type='hidden' name='materialId' id='materialId"+rowIndex+ "'><input name='materialCode' id='materialCode" +rowIndex+ "' size='6' ondblclick='openWinMaterialForInvoice("+rowIndex+",800,600,1)'/></td>";
	col = row.insertCell(2);
	col.innerHTML = "<td><input type='text' name='materialName' id='materialName"+rowIndex+"' readonly='true' size='6'></td>";
	col = row.insertCell(3);
	col.innerHTML = "<td><input type='text' name='spec' id='spec"+rowIndex+"' readonly='true' size='6'/></td>";
	col = row.insertCell(4);
	col.innerHTML = "<td><input type='text' name='assistantAttributeTypeName' id='assistantAttributeTypeName"+rowIndex+"' readonly='true' size='6'/></td>";
	col = row.insertCell(5);
	col.innerHTML = "<td><input type='text' name='basicMeasureUnit' id='basicMeasureUnit"+rowIndex+"' readonly='true' size='6' /></td>";
	col = row.insertCell(6);
	col.innerHTML = "<td><input type='text' name='basicUnitAmount' id='basicCost"+rowIndex+"' readonly='true' size='6' /></td>";
	col = row.insertCell(7);
	col.innerHTML = "<td><input type='hidden' name='measureUnitId' id='measureUnitId" +rowIndex+ "'/><input type='text' name='measureUnitName' id='measureUnitName"+rowIndex+"' ondblclick='openWinMeasureUnit(" + rowIndex + ",800,600,0)' size='6'/></td>";
	col = row.insertCell(8);
	col.innerHTML = "<td><input type='text' name='amount' id='cost"+rowIndex+"' onblur='amount1("+rowIndex+")' size='6'/></td>";
	col = row.insertCell(9);
	col.innerHTML = "<td><input type='text' name='unitPrice' id='unitPrice"+rowIndex+"' onblur='up("+rowIndex+")' size='6'/></td>";
	col = row.insertCell(10);
	col.innerHTML = "<td><input type='text' name='discount' id='discount"+rowIndex+"' onblur='up("+rowIndex+")' size='6'/></td>";
	col = row.insertCell(11);
	col.innerHTML = "<td><input type='text' name='dutyFrank' id='dutyFrank"+rowIndex+"' onblur='up("+rowIndex+")' size='6'/></td>";
	col = row.insertCell(12);
	col.innerHTML = "<td><input type='text' name='totalUnitPrice' id='totalUnitPrice"+rowIndex+"' onblur='tup("+rowIndex+")' size='6'/></td>";
	col = row.insertCell(13);
	col.innerHTML = "<td><input type='text' name='realTotalUnitPrice' id='realTotalUnitPrice"+rowIndex+"' onblur='rtup("+rowIndex+")' size='6'/></td>";
	col = row.insertCell(14);
	col.innerHTML = "<td><input type='text' name='money' id='money"+rowIndex+"' readonly='true' size='6'/></td>";
	col = row.insertCell(15);
	col.innerHTML = "<td><input type='text' name='moneyBw' id='moneyBw"+rowIndex+"' readonly='true' size='6'/></td>";
	col = row.insertCell(16);
	col.innerHTML = "<td><input type='text' name='discountPrice' id='discountPrice"+rowIndex+"' readonly='true' size='6'/></td>";
	col = row.insertCell(17);
	col.innerHTML = "<td><input type='text' name='discountPriceBw' id='discountPriceBw"+rowIndex+"' readonly='true' size='6'/></td>";
	col = row.insertCell(18);
	col.innerHTML = "<td><input type='text' name='dutyPrice' id='dutyPrice"+rowIndex+"' readonly='true' size='6'/></td>";
	col = row.insertCell(19);
	col.innerHTML = "<td><input type='text' name='dutyPriceBw' id='dutyPriceBw"+rowIndex+"' readonly='true' size='6'/></td>";
	col = row.insertCell(20);
	col.innerHTML = "<td><input type='text' name='totalMoney' id='totalMoney"+rowIndex+"' readonly='true' size='6'/></td>";
	col = row.insertCell(21);
	col.innerHTML = "<td><input type='text' name='totalMoneyBw' id='totalMoneyBw"+rowIndex+"' readonly='true' size='6'/></td>";
	col = row.insertCell(22);
	col.innerHTML = "<td><input type='text' name='orderTotalUnitPrice' id='orderTotalUnitPrice"+rowIndex+"' readonly='true' size='6'/></td>";
	col = row.insertCell(23);
	col.innerHTML = "<td><input type='text' name='remark' id='remark"+rowIndex+"' size='6'/></td>";
	col = row.insertCell(24);
	col.innerHTML = "<td><input type='text' name='sourceBillCode' id='sourceBillCode"+rowIndex+"' readonly='true' size='6'/></td>";
	col = row.insertCell(25);
	col.innerHTML = "<td><input type='text' name='sourceOrderCode' id='sourceOrderCode"+rowIndex+"' readonly='true' size='6'/></td>";
}

function openWinMaterialForInvoice(index, width, height, scroll) {
	var truelink = "selectMaterialForInvoice.action?rowIndex="+index;
	openWin(truelink, null, width, height, scroll);
}
function openWinMeasureUnit(index, width, height, scroll){
	var truelink = "selectMeasure.action?rowIndex="+index;
	openWin(truelink, null, width, height, scroll);
}

function childToParentForInvoice(index,id,childId,name,childName,code,childCode,spec,childSpec,assistantAttributeTypeName,childAssistantAttributeTypeName,basicMeasureUnitName,childBasicMeasureUnitName,measureUnitId,childMeasureUnitId) {
	var t = window.opener.document.getElementById("mbody");	
	//物料id
	var parentId = window.opener.document.getElementById(id + index);
	//物料name
	var parentName = window.opener.document.getElementById(name+ index);
	//物料code
	var parentCode = window.opener.document.getElementById(code + index);
	//规格型号
	var parentSpec = window.opener.document.getElementById(spec + index);
	//辅助属性
	var parentAttribute = window.opener.document.getElementById(assistantAttributeTypeName + index);
	//基本计量单位
	var parentBasicMeasureUnitName =  window.opener.document.getElementById(basicMeasureUnitName+ index);
	//基本单位数量
	var parentBasicUnitAmount =  window.opener.document.getElementById('basicCost'+ index);
	//单位
	var parentMeasureUnitName = window.opener.document.getElementById('measureUnitName'+ index);
	var parentMeasureUnitId = window.opener.document.getElementById(measureUnitId + index);
	//数量
	var parentAccount =  window.opener.document.getElementById('cost'+ index);
	//单价
	var unitPrice = window.opener.document.getElementById('unitPrice'+index);
	//折扣率
	var discount = window.opener.document.getElementById('discount'+index);
	//税率
	var dutyFrank = window.opener.document.getElementById('dutyFrank'+index);
	//含税单价
	var totalUnitPrice = window.opener.document.getElementById('totalUnitPrice'+index);
	//实际含税单价	
	var realTotalUnitPrice = window.opener.document.getElementById('realTotalUnitPrice'+index);
	//金额
	var money = window.opener.document.getElementById('money'+index);
	//金额本位
	var moneyBw = window.opener.document.getElementById('moneyBw'+index);
	//折扣额
	var discountPrice = window.opener.document.getElementById('discountPrice'+index);
	//折扣额本位
	var discountPriceBw = window.opener.document.getElementById('discountPriceBw'+index);
	//税额
	var dutyPrice = window.opener.document.getElementById('dutyPrice'+index);
	//税额本位
	var dutyPriceBw = window.opener.document.getElementById('dutyPriceBw'+index);
	//价格合计
	var totalMoney = window.opener.document.getElementById('totalMoney'+index);
	//价格合计本位
	var totalMoneyBw = window.opener.document.getElementById('totalMoneyBw'+index);
	
	parentId.value = childId;
	parentName.value = childName;
	parentCode.value = childCode;
	parentSpec.value = childSpec;
	
	parentAttribute.value = childAssistantAttributeTypeName;
	parentBasicMeasureUnitName.value = childBasicMeasureUnitName;
	parentMeasureUnitName.value = childBasicMeasureUnitName;	
	parentMeasureUnitId.value = childMeasureUnitId;
	
	//基本单位数量
	parentBasicUnitAmount.value = '0.00';
	//数量
	parentAccount.value = '0.00';
	//单价
	unitPrice.value='0.00';
	//折扣率
	discount.value  = '0.00' ;
	//税率
	dutyFrank.value =17; 
	//含税单价
	totalUnitPrice.value='0.00' ;
	//实际含税单价
	realTotalUnitPrice.value='0.00' ;
	//金额
	money.value = '0.00';
	//金额本位
	moneyBw.value = '0.00';
	//折扣额
	discountPrice.value = '0.00';
	//折扣额本位
	discountPriceBw.value = '0.00';
	//税额
	dutyPrice.value='0.00';
	//税额本位
	dutyPriceBw.value='0.00';
	//价格合计
	totalMoney.value='0.00'
	//价格合计本位
	totalMoneyBw.value='0.00'
	window.close();
}


//明细--数量/单价事件触发函数
function amount1(rowIndex){
	//汇率
	var rate = document.getElementById('rate');
	var vrate = parseFloat(rate.value);
	
	//物料数量
	var cost = document.getElementById('cost'+rowIndex);
	var vcost = parseFloat(cost.value);
	
	//物料单价
	var unitPrice = document.getElementById('unitPrice'+rowIndex);
	var vunitPrice = parseFloat(unitPrice.value);
	//折扣率
	var discount = document.getElementById('discount'+rowIndex);
	var vdiscount  = parseFloat(discount.value);
	//税率
	var dutyFrank = document.getElementById('dutyFrank'+rowIndex);
	var vdutyFrank = parseFloat(dutyFrank.value);
	//含税单价
	var totalUnitPrice = document.getElementById('totalUnitPrice'+rowIndex);
	var vtotalUnitPrice = parseFloat(totalUnitPrice.value);
	//实际含税单价
	var realTotalUnitPrice = document.getElementById('realTotalUnitPrice'+rowIndex);
	var vrealTotalUnitPrice = parseFloat(realTotalUnitPrice.value);
	//金额
	var money = document.getElementById('money'+rowIndex);
	var vmoney = parseFloat(money.value);
	//金额本位
	var moneyBw = document.getElementById('moneyBw'+rowIndex);
	var vmoneyBw = parseFloat(moneyBw.value);
	//折扣额
	var discountPrice = document.getElementById('discountPrice'+rowIndex);
	var vdiscountPrice = parseFloat(discountPrice.value);
	//折扣额本位
	var discountPriceBw = document.getElementById('discountPriceBw'+rowIndex);
	var vdiscountPriceBw = parseFloat(discountPriceBw.value);
	//税额
	var dutyPrice = document.getElementById('dutyPrice'+rowIndex);
	var vdutyPrice = document.getElementById(dutyPrice.value);
	//税额
	var dutyPriceBw = document.getElementById('dutyPriceBw'+rowIndex);
	var vdutyPriceBw = document.getElementById(dutyPriceBw.value);
	//价税合计
	var totalMoney = document.getElementById('totalMoney'+rowIndex);
	var vtotalMoney = parseFloat(totalMoney.value);
	//价税合计本位
	var totalMoneyBw = document.getElementById('totalMoneyBw'+rowIndex);
	var vtotalMoneyBw = parseFloat(totalMoneyBw.value);
	
	if(vcost>0.0){
		getBasicCostInfo(rowIndex);
	}
	
	if(vcost>0.0 && vunitPrice>0.0){
		
		//getBasicCostInfo(rowIndex);
		//金额 ＝ 数量*单价*（1-折扣率）/100
		money.value = vcost*vunitPrice*(1-vdiscount/100) ;
		moneyBw.value = money.value*vrate;
		//折扣额＝数量*含税单价*折扣率/100
		discountPrice.value = vcost*vtotalUnitPrice*vdiscount/100;
		discountPriceBw.value = discountPrice.value*vrate;
		//税额＝金额*税率/100
		dutyPrice.value = money.value*vdutyFrank/100;
		dutyPriceBw.value = dutyPrice.value*vrate;
		//价税合计＝数量*实际含税单价
		totalMoney.value = vcost*vrealTotalUnitPrice;
		totalMoneyBw.value = totalMoney.value*vrate;
	}

}

function up(rowIndex){
	//物料单价
	var unitPrice = document.getElementById('unitPrice'+rowIndex);
	var vunitPrice = parseFloat(unitPrice.value);
	//折扣率
	var discount = document.getElementById('discount'+rowIndex);
	var vdiscount  = parseFloat(discount.value);
	//税率
	var dutyFrank = document.getElementById('dutyFrank'+rowIndex);
	var vdutyFrank = parseFloat(dutyFrank.value);
	//含税单价
	var totalUnitPrice = document.getElementById('totalUnitPrice'+rowIndex);
	var vtotalUnitPrice = parseFloat(totalUnitPrice.value);
	//实际含税单价
	var realTotalUnitPrice = document.getElementById('realTotalUnitPrice'+rowIndex);
	var vrealTotalUnitPrice = parseFloat(realTotalUnitPrice.value);
	
	totalUnitPrice.value = vunitPrice*(1+vdutyFrank/100);
	realTotalUnitPrice.value = totalUnitPrice.value*(1-vdiscount/100); 
	
	amount1(rowIndex)
}

function tup(rowIndex){
	//物料单价
	var unitPrice = document.getElementById('unitPrice'+rowIndex);
	var vunitPrice = parseFloat(unitPrice.value);
	//折扣率
	var discount = document.getElementById('discount'+rowIndex);
	var vdiscount  = parseFloat(discount.value);
	//税率
	var dutyFrank = document.getElementById('dutyFrank'+rowIndex);
	var vdutyFrank = parseFloat(dutyFrank.value);
	//含税单价
	var totalUnitPrice = document.getElementById('totalUnitPrice'+rowIndex);
	var vtotalUnitPrice = parseFloat(totalUnitPrice.value);
	//实际含税单价
	var realTotalUnitPrice = document.getElementById('realTotalUnitPrice'+rowIndex);
	var vrealTotalUnitPrice = parseFloat(realTotalUnitPrice.value);
	
	unitPrice.value = vtotalUnitPrice/(1+vdutyFrank/100);
	realTotalUnitPrice.value = vtotalUnitPrice*(1-vdiscount/100);
	
	amount1(rowIndex)
}

function rtup(rowIndex){
	//物料单价
	var unitPrice = document.getElementById('unitPrice'+rowIndex);
	var vunitPrice = parseFloat(unitPrice.value);
	//折扣率
	var discount = document.getElementById('discount'+rowIndex);
	var vdiscount  = parseFloat(discount.value);
	//税率
	var dutyFrank = document.getElementById('dutyFrank'+rowIndex);
	var vdutyFrank = parseFloat(dutyFrank.value);
	//含税单价
	var totalUnitPrice = document.getElementById('totalUnitPrice'+rowIndex);
	var vtotalUnitPrice = parseFloat(totalUnitPrice.value);
	//实际含税单价
	var realTotalUnitPrice = document.getElementById('realTotalUnitPrice'+rowIndex);
	var vrealTotalUnitPrice = parseFloat(realTotalUnitPrice.value);
	
	totalUnitPrice.value = vrealTotalUnitPrice/(1-vdiscount/100);
	unitPrice.value = totalUnitPrice.value/(1+vdutyFrank/100);
	
	amount1(rowIndex)
}